//
//  Account.m
//  MHO
//
//  Created by papo on 2/20/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "Account.h"
#import "Lib.h"
#import "Contants.h"
#import "ServiceLib.h"
#import "SBJson.h"
#import "NSString+HTML.h"

@implementation Account

+ (Account*)accountWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

+ (ServiceResult*)loginWithInfo:(NSDictionary *)dict {
    NSString* urlStr = [NSString stringWithFormat:@"%@&%@=%@&%@=%@&%@=%@&%@=%@",
                        [Contants urlForKey:kServiceUserLogin],
                        kAccountServiceId, [Lib getServiceId],
                        kAccountServiceKey, [Lib getServiceKey],
                        kAccountEmail, [dict objectForKey:kAccountEmail],
                        kAccountPassword, [dict objectForKey:kAccountPassword]];

    NSString* retVal = [ServiceLib sendGetRequest:urlStr];
    BOOL success = NO;
    NSString* message;
    if (retVal) {
        SBJsonParser* parser = [[SBJsonParser alloc] init];
        NSDictionary* result = [parser objectWithString:retVal];
        if (result && [result isKindOfClass:[NSDictionary class]]) {
            NSString* ret = [result objectForKey:@"Ret"];
            message = [result objectForKey:@"Message"];

            if ([ret integerValue] == -48) {
                
            }
            else if ([ret integerValue] == -50) {
                
            }
            else if ([ret integerValue] == -53) {
                
            }else if ([ret integerValue] == -99) {
                
            }
            else    // success
            {
                NSDictionary* data = [result objectForKey:@"Data"];
                if (data && [data isKindOfClass:[NSDictionary class]]) {
                    for (NSString* key in [data allKeys]) {
                        if (key) {
                            NSString* object = [NSString stringWithFormat:@"%@", [data objectForKey:key]];
                            [Lib setString:object forKey:key];
                        }
                    }

                    success = YES;
                }
            }
        }
    }
    
    [Lib setUserLogin:success];

    return [ServiceResult result:success message:message];
}

+ (ServiceResult*)registerUserWithDictionary:(NSDictionary *)dict {
    NSString* urlStr = [NSString stringWithFormat:@"%@&%@=%@&%@=%@&%@=%@&%@=%@&%@=%@&%@=%@",
                        [Contants urlForKey:kServiceUserRegister],
                        kAccountServiceId, [Lib getServiceId],
                        kAccountServiceKey, [Lib getServiceKey],
                        kAccountEmail, [dict objectForKey:kAccountEmail],
                        kAccountPassword, [dict objectForKey:kAccountPassword],
                        @"phone", [dict objectForKey:@"phone"],
                        @"pemail", [dict objectForKey:@"pemail"]
                        ];
    BOOL success = NO;
    NSString* message;
    NSString* retVal = [ServiceLib sendGetRequest:urlStr];
    if (retVal) {
        SBJsonParser* parser = [[SBJsonParser alloc] init];
        NSDictionary* result = [parser objectWithString:retVal];
        if (result && [result isKindOfClass:[NSDictionary class]]) {
            NSString* ret = [result objectForKey:@"Ret"];
            message = [result objectForKey:@"Message"];
            
            if (ret.integerValue > 0)   // success
            {
                NSDictionary* data = [result objectForKey:@"Data"];
                if (data && [data isKindOfClass:[NSDictionary class]]) {
                    for (NSString* key in [data allKeys]) {
                        if (key) {
                            NSString* object = [NSString stringWithFormat:@"%@", [data objectForKey:key]];
                            [Lib setString:object forKey:key];
                        }
                    }
                    
                    success = YES;
                }
            }
        }
    }
    [Lib setUserLogin:success];

    return [ServiceResult result:success message:message];
}

+ (BOOL)updateInfoWithDictionary:(NSDictionary *)dict {
    return NO;
}

+ (Account*)getCurrentUserInfo {

    NSString* urlStr = [NSString stringWithFormat:@"%@&%@=%@&%@=%@&%@=%@&%@=%@",
                        [Contants urlForKey:kServiceUserGetInfo],
                        kAccountServiceId, [Lib getServiceId],
                        kAccountServiceKey, [Lib getServiceKey],
                        @"email", [Lib stringForKey:kAccountModelEmail],
                        @"token", [Lib stringForKey:kAccountRequestToken]];

    NSString* retVal = [ServiceLib sendGetRequest:urlStr];
    if (retVal) {
        SBJsonParser* parser = [[SBJsonParser alloc] init];
        NSDictionary* result = [parser objectWithString:retVal];
        if (result && [result isKindOfClass:[NSDictionary class]]) {
            NSString* ret = [result objectForKey:@"Ret"];
            NSString* message = [result objectForKey:@"Message"];
            if (ret.integerValue > 0) {
                NSDictionary* data = [result objectForKey:@"Data"];
                Account* user = [Account accountWithDictionary:data];
                return user;
            }
            else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [Lib showMessage:message title:@""];
                });
            }
        }
    }
    return nil;
}

- (id)initWithDictionary:(NSDictionary *)dict {
    if (self = [super init]) {
        if (dict) {
            for (NSString* key in dict.allKeys) {
                NSString* object = [dict objectForKey:key];
                [Lib setString:object forKey:key];
            }
        }
    }
    return self;
}

- (void)getUserInfo {
    
}

+ (ServiceResult*)changePassword:(NSString *)current newPass:(NSString *)newPass {
    NSString* serverUrl = @"http://api.mho.vn/services/account?type=changepass";
    serverUrl = [NSString stringWithFormat:@"%@&serviceid=%@&servicekey=%@&uid=%@&opwd=%@&npwd=%@", serverUrl, [Lib getServiceId], [Lib getServiceKey], [Lib stringForKey:@"AccountId"], current, newPass];
    
    NSString* retVal = [ServiceLib sendGetRequest:serverUrl];
    NSString* message = @"Lỗi hệ thống";
    BOOL bResult = NO;
    if (retVal) {
        SBJsonParser* parser = [[SBJsonParser alloc] init];
        NSDictionary* result = [parser objectWithString:retVal];
        if (result && [result isKindOfClass:[NSDictionary class]]) {
            message = [result objectForKey:@"Message"];
        }
    }
    
    return [ServiceResult result:bResult message:message];
}
@end
